package com.telecom.restaurant.controllor.manage;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.ManageThreadLocal;
import com.telecom.restaurant.dao.AdminDao;
import com.telecom.restaurant.dao.RestaurantDao;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.model.AdminPO;
import com.telecom.restaurant.model.RestaurantPO;
import com.telecom.restaurant.service.AdminService;
import com.telecom.restaurant.service.PictureService;
import com.telecom.restaurant.util.EncryptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by qiqi on 2019/11/23.
 */
@Controller
@RequestMapping("/restaurant")
public class RestaurantController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestaurantDao restaurantDao;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private AdminService adminService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping("/admin/login")
    @ResponseBody
    public DataResult adminLogin(String username, String password) {

        AdminPO admin = adminDao.selectByUsername(username);
        if (admin == null) {
            DataResult dataResult = DataResultConstant.WRONG_PASSWORD;
            return dataResult;
        }
        String md5Password = EncryptUtil.md5(password);
        if (!admin.getPassword().equals(md5Password)) {
            DataResult dataResult = DataResultConstant.WRONG_PASSWORD;
            return dataResult;
        }

        DataResult dataResult = DataResultConstant.buildSuccessResult(admin.getUsername());

        String newToken = adminService.generateAdminToken(admin.getId(), admin.getRestaurantId());

        dataResult.setToken(newToken);
        return dataResult;
    }

    @RequestMapping("/getInfo")
    @ResponseBody
    public DataResult getRestaurantInfo() {

        Long restaurantId = ManageThreadLocal.restaurantId.get();
        RestaurantPO po = restaurantDao.selectByPrimaryKey(restaurantId);
        po.setPicture(pictureService.getPicturePath(po.getPicture()));
        DataResult dataResult = DataResultConstant.buildSuccessResult(po);
        return dataResult;
    }

    @RequestMapping("/saveInfo")
    @ResponseBody
    public DataResult saveInfo(RestaurantPO restaurant) {

        Long restaurantId = ManageThreadLocal.restaurantId.get();
        if (restaurant.getId() != null && !restaurant.getId().equals(restaurantId)) {
            return DataResultConstant.WRONG_PARAM;

        }
        if (restaurant.getId() == null) {
            restaurant.setId(restaurantId);
        }

        restaurantDao.updateByPrimaryKeySelective(restaurant);

        DataResult dataResult = DataResultConstant.buildSuccessResult(null);
        return dataResult;
    }


}
